﻿#include "AddCell.h"

AddRemoveCell::AddRemoveCell(JistDocument *document, int index, JistTableView *tableView, JistCommonData *data)
    : m_pDocument(document)
    , m_pTableView(tableView)
    , m_Index(index)
    , m_OwnData(0)
{
    JistCommonModel *dataModel = (JistCommonModel*)(m_pTableView->model());
    // 셀 복사 관리시
    if(tableView->m_eChangeMode == JistTableView::CELLLIST_CHANGE){
        m_OwnData = new JistCommonData();
        JistCommonData srcData;
        if(data)
            srcData = *data;
        else
            srcData = dataModel->m_pList->at(m_Index);
        *m_OwnData = srcData;
        m_OwnData->copyData(srcData);
//        m_Cell->m_pIItem = new ImageItem(&m_Cell->m_img);
//        m_Cell->m_pIItem->setZValue(srcData.m_pIItem->zValue());
//        m_Cell->m_pIItem->setPos(srcData.m_pIItem->pos());
//        m_Cell->m_pIItem->m_FlipH = srcData.m_pIItem->m_FlipH;
//        m_Cell->m_pIItem->m_FlipV = srcData.m_pIItem->m_FlipV;
//        m_Cell->m_pIItem->setOpacity(srcData.m_pIItem->opacity());
//        m_Cell->m_pIItem->setRotation(srcData.m_pIItem->rotation());
//        m_Cell->m_pIItem->setScale(srcData.m_pIItem->scale());
//        m_Cell->m_pIItem->m_Image = srcData.m_pIItem->m_Image;
    }
}

AddRemoveCell::~AddRemoveCell()
{
//    if(m_OwnData)
//        delete m_OwnData;
}

void AddRemoveCell::addSprite()
{
    //const int currentIndex = m_pTableView->currentIndex().row();
    JistCommonModel *dataModel = (JistCommonModel*)(m_pTableView->model());
    //m_pDocument->m_pSceneFrameFigure->m_canvas.addItem(m_Cell->m_pIItem);
    m_pDocument->m_pSceneFrameFigure->addItem(m_OwnData->m_pIItem);

    dataModel->insertData(m_Index, m_OwnData);
    //m_Cell = 0;
    m_pTableView->refineView(m_Index);
    // 레이어 설정
//    for(int k = m_Index + 1; k < dataModel->m_pList->count(); k++)
//        dataModel->m_pList->value(k).m_pIItem->setZValue(k);
}

void AddRemoveCell::removeSprite()
{
    JistCommonModel *dataModel = (JistCommonModel*)(m_pTableView->model());
    JistCommonData srcData = dataModel->m_pList->at(m_Index);
    m_pDocument->m_pSceneFrameFigure->m_canvas.removeItem(srcData.m_pIItem);
    dataModel->takeAt(m_Index);
    int selIndex = m_Index;
    if (m_Index == dataModel->rowCount())
        selIndex = m_Index - 1;
    m_pTableView->refineView(selIndex);
}

void AddRemoveCell::addCell()
{
    //const int currentIndex = m_pTableView->currentIndex().row();
    JistCommonModel *dataModel = (JistCommonModel*)(m_pTableView->model());
    //m_pDocument->m_pSceneFrameFigure->m_canvas.addItem(m_Cell->m_pIItem);
    m_pDocument->m_pSceneFrameFigure->addItem(m_OwnData->m_pIItem);

    dataModel->insertData(m_Index, m_OwnData);
    //m_Cell = 0;
    // 레이어 설정
    int aniId = m_pDocument->m_AnimaionTableWidget->m_iSelectIndex;
    for(int k = 0; k < dataModel->m_pList->count(); k++){
        dataModel->m_pList->value(k).m_pIItem->setZValue(aniId + k * 0.0001f);
        dataModel->m_pList->value(k).m_pIItem->setId(k);
    }
    m_pTableView->refineView(m_Index);
    JistCommonModel* sceneModel = (JistCommonModel*)(m_pDocument->m_SceneTableWidget->model());
    sceneModel->setData(m_pDocument->m_SceneTableWidget->currentIndex(), dataModel->m_pList->count(), Qt::UserRole+4);
}

void AddRemoveCell::removeCell()
{
    // 지울때 다운시 지우기전 데이터 보존
    m_pDocument->saveTempFile(m_pDocument->getDocName());
    JistCommonModel *dataModel = (JistCommonModel*)(m_pTableView->model());
    JistCommonData srcData = dataModel->m_pList->at(m_Index);
    m_pDocument->m_pSceneFrameFigure->m_canvas.removeItem(srcData.m_pIItem);
    dataModel->takeAt(m_Index);
    int selIndex = m_Index;
    if (m_Index == dataModel->rowCount())
        selIndex = m_Index - 1;
    // 레이어 설정
    int aniId = m_pDocument->m_AnimaionTableWidget->m_iSelectIndex;
    for(int k = 0; k < dataModel->m_pList->count(); k++){
        dataModel->m_pList->value(k).m_pIItem->setZValue(aniId + k * 0.0001f);
        dataModel->m_pList->value(k).m_pIItem->setId(k);
    }
    m_pTableView->refineView(selIndex);
    JistCommonModel* sceneModel = (JistCommonModel*)(m_pDocument->m_SceneTableWidget->model());
    sceneModel->setData(m_pDocument->m_SceneTableWidget->currentIndex(), dataModel->m_pList->count(), Qt::UserRole+4);
}

// 그림자 넣기
void AddRemoveCell::addShadowCell()
{
    //const int currentIndex = m_pTableView->currentIndex().row();
    JistCommonModel *dataModel = (JistCommonModel*)(m_pTableView->model());
    //m_pDocument->m_pSceneFrameFigure->m_canvas.addItem(m_Cell->m_pIItem);
    m_pDocument->m_pSceneFrameFigure->addItem(m_OwnData->m_pIItem);

    dataModel->insertData(m_Index, m_OwnData);
    //m_Cell = 0;
    // 레이어 설정
    int aniId = m_pDocument->m_AnimaionTableWidget->m_iSelectIndex;
    for(int k = 0; k < dataModel->m_pList->count(); k++){
        dataModel->m_pList->value(k).m_pIItem->setZValue(aniId + k * 0.0001f);
        dataModel->m_pList->value(k).m_pIItem->setId(k);
    }
    m_pTableView->refineView(m_Index);
    JistCommonModel* sceneModel = (JistCommonModel*)(m_pDocument->m_SceneTableWidget->model());
    sceneModel->setData(m_pDocument->m_SceneTableWidget->currentIndex(), dataModel->m_pList->count(), Qt::UserRole+4);
}

// 신 복사
void AddRemoveCell::addCellList()
{
    JistCommonModel *dataModel = (JistCommonModel*)(m_pTableView->model());
    JistCommonData data;
    data.m_bVisible = true;
    //data.m_Name = "Scene";
    dataModel->insertData(m_Index, m_OwnData);
    for(int k = 0; k < m_SceneData.count(); k++){
        m_pDocument->m_pSceneFrameFigure->addItem(m_SceneData.at(k).m_pIItem);
    }
    m_pDocument->m_CellData[m_pDocument->m_iCurrentSceneDataIndex].insert(m_Index, m_SceneData);
    m_pDocument->m_CellListModel.setDataList(&m_pDocument->m_CellData[m_pDocument->m_iCurrentSceneDataIndex][m_Index]);
    //m_Cell = 0;
    m_pTableView->refineViewNoUndo(m_Index);
}

void AddRemoveCell::removeCellList()
{
    // 지울때 다운시 지우기전 데이터 보존
    m_pDocument->saveTempFile(m_pDocument->getDocName());
    JistCommonModel *dataModel = (JistCommonModel*)(m_pTableView->model());
    JistCommonData srcData = dataModel->m_pList->at(m_Index);
    //m_pDocument->m_pSceneFrameFigure->m_canvas.removeItem(srcData.m_pIItem);

    dataModel->takeAt(m_Index);
    for(int k = 0; k < m_SceneData.count(); k++){
        m_pDocument->m_pSceneFrameFigure->m_canvas.removeItem(m_SceneData.at(k).m_pIItem);
        // 셀리스트 삭제 버그 수정
        m_pDocument->m_CellData[m_pDocument->m_iCurrentSceneDataIndex][m_Index].removeFirst();
    }
    m_pDocument->m_CellData[m_pDocument->m_iCurrentSceneDataIndex].remove(m_Index);

    int selIndex = m_Index;
    if (m_Index == dataModel->rowCount())
        selIndex = m_Index - 1;
    if(selIndex >= 0)
        m_pDocument->m_CellListModel.setDataList(&m_pDocument->m_CellData[m_pDocument->m_iCurrentSceneDataIndex][selIndex]);
    else
        m_pDocument->changeCellList(selIndex);

    m_pTableView->refineViewNoUndo(selIndex);
}

//
void AddRemoveCell::copyAnimation()
{
    JistCommonModel *dataModel = (JistCommonModel*)(m_pTableView->model());
    JistCommonData data;
    data.m_bVisible = true;
    data.m_Name = m_OwnData->m_Name;//"Animation";
    dataModel->insertData(m_Index, m_OwnData);
    // 셀리스트뷰에 대한 갱신
    QList< QVector< QList< JistCommonData > > > tempCellList = m_pDocument->m_CellData.toList();
    tempCellList.insert(m_Index, m_AniData );
    m_pDocument->m_CellData = tempCellList.toVector();
    m_pDocument->m_CellListModel.setDataList(&m_pDocument->m_CellData[m_Index][m_pDocument->m_iCurrentCellDataIndex]);
    m_pDocument->m_CellTableWidget->refineView(0);
    // 신리스트뷰에 대한 갱신
    QList< QList < JistCommonData> > tempList = m_pDocument->m_SceneData.toList();
    tempList.insert(m_Index, m_SceneData);
    m_pDocument->m_SceneData = tempList.toVector();
    m_pDocument->m_SceneListModel.setDataList(&m_pDocument->m_SceneData[m_Index]);
    m_pTableView->refineViewNoUndo(m_Index);
}

void AddRemoveCell::removeAnimation()
{
    // 지울때 다운시 지우기전 데이터 보존
    m_pDocument->saveTempFile(m_pDocument->getDocName());
    JistCommonModel *dataModel = (JistCommonModel*)(m_pTableView->model());
    JistCommonData srcData = dataModel->m_pList->at(m_Index);

    dataModel->takeAt(m_Index);

    for(int k = 0; k < m_AniData.count(); k++){
        int cellCnt = m_AniData.at(k).count();
        for(int j = 0; j < cellCnt; j++){
            m_pDocument->m_pSceneFrameFigure->m_canvas.removeItem(m_AniData[k][j].m_pIItem);
        }
    }

    m_pDocument->m_CellData.remove(m_Index);
    m_pDocument->m_SceneData.remove(m_Index);

    int selIndex = m_Index;
    if (m_Index == dataModel->rowCount())
        selIndex = m_Index - 1;

    if(selIndex >= 0){
        m_pDocument->m_SceneListModel.setDataList(&m_pDocument->m_SceneData[selIndex]);
        m_pDocument->m_CellListModel.setDataList(&m_pDocument->m_CellData[selIndex][0]);
    }
    else{
        m_pDocument->clearAniList();
    }

    m_pTableView->refineViewNoUndo(selIndex);
}


CopyAnimation::CopyAnimation(JistDocument *document, int index, JistTableView *tableView,  QVector< QList < JistCommonData > > *aniData, QList < JistCommonData> *sceneData, JistCommonData *data)
    : AddRemoveCell(document, index, tableView)
{
    setText(QCoreApplication::translate("Undo Commands", "Add Animation(s)"));
    if(!m_OwnData)
        m_OwnData = new JistCommonData();
    m_OwnData->copyData(*data);
    m_AniData = *aniData;
    for(int k = 0; k < aniData->count(); k++){
        int cellCnt = aniData->at(k).count();
        for(int j = 0; j < cellCnt; j++){
            m_AniData[k][j].copyData(aniData->at(k).at(j));
            aniData->at(k).at(j).m_pIItem->setZValue(index + 1 + j * 0.0001f);
        }
    }

    m_SceneData = *sceneData;
    for(int k = 0; k < m_SceneData.count(); k++){
        m_SceneData[k].copyData(sceneData->at(k));
    }

}
